module.exports = function(sysData){
    function init(sysData){
        sysData = sysData || new Date();
        sysData = /^\d|-+$/.test(sysData) ? new Date(-(-sysData)) : sysData;
        if(typeof sysData == 'number') sysData = new Date(-(-sysData));
        sysData.unix = Math.round(sysData.getTime() / 1000);
        sysData.Format = function(fmt){
            fmt = fmt || 'yyyy-MM-dd hh:mm:ss';
            var o = {
                "M+": sysData.getMonth() + 1, //月份 
                "d+": sysData.getDate(), //日 
                "h+": sysData.getHours(), //小时 
                "m+": sysData.getMinutes(), //分 
                "s+": sysData.getSeconds(), //秒 
                "q+": Math.floor((sysData.getMonth() + 3) / 3), //季度 
                "S": sysData.getMilliseconds() //毫秒 
            };
            fmt = fmt.replace(/(y+)/,function($0,$1){
                return (sysData.getFullYear() + "").substr(4 - $1.length);
            })
            for (var k in o) {
                fmt = fmt.replace(new RegExp("(" + k + ")"), function($0,$1){
                    return ($1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length));
                });
            }
            return fmt;
        }
        sysData.prev = function(ms){
            ms = ms || 1000 * 60 * 60 * 24;
            return init(sysData.getTime() - ms);
        }
        sysData.parse = function(d,type){
            var day = [],time = [];
            switch(type){
                case 'date':
                    day = d.split('-');
                    break;
                case 'time':
                    time = d.split(':');
                    break;
                default:
                    d = d.split(' ');
                    day = d[0].split('-'),time = d[1].split(':');
                    break;
            }
            if(day[0]) sysData.setFullYear(day[0]);
            if(day[1]) sysData.setMonth(day[1] - 1);
            if(day[2]) sysData.setDate(day[2]);
            if(time[0]) sysData.setHours(time[0]);
            if(time[1]) sysData.setMinutes(time[1]);
            if(time[2]) sysData.setSeconds(time[2]);
            return sysData;
        }
        sysData.contDown = function(d){
            d = d.split(' ');
            var day = d[0].split('-'),time = d[1].split(':'),end = new Date();
            end.setFullYear(day[0],day[1] - 1,day[2]);
            end.setHours(time[0]);
            end.setMinutes(time[1]);
            end.setSeconds(time[2]);
            var countDown = parseInt((end.getTime() - sysData.getTime()) / 1000),countDownString = '';
            var dayNum = 24 * 60 * 60 * 1000, hoursNum = 60 * 60 * 1000, miniteNum = 60 * 1000;
            var day = Math.floor(countDown / dayNum);
            if(day > 0) countDownString = day + '天';
            countDown = countDown % dayNum;
            var hours = Math.floor(countDown / hoursNum);
            if (hours > 0) countDownString += hours + '时';
            countDown = countDown % hoursNum;
            var minite = Math.floor(countDown / miniteNum);
            if (minite > 0) countDownString += minite + '分';
            countDown = countDown % miniteNum;
            var second = Math.floor(countDown / 1000);
            if (second > 0) countDownString += second + '秒';
            return countDownString;
        }
        sysData.only = function(){
            var n = this.getTime().toString();
            n = -(-n.substr((n.length - 10),9));
            return n;
        }
        sysData.age = function(y,m,d){
            var age = 0;
            age = this.getFullYear() - y;
            if(this.getMonth() + 1 > m){
                age ++;
            }else if(this.getMonth() + 1 == m){
                if(this.getDate() >= d){
                    age ++;
                }
            }
            return age;
        }
        sysData.weekDay = function(){
            return ['日','一','二','三','四','五','六'][this.getDay()];
        }
        sysData.toLocalString = function(){
            return this.Format('yyyy-MM-dd hh:mm:ss');
        }
        return sysData;
    }
    return init(sysData);
}